import json

import requests

from utils.log_util import logger
from utils.read import base_data

# api_root_url = base_data.read_ini()['host']['douban_url']
api_root_url = base_data.read_ini()['host']['api_sit_url']



class RestClient():
    def __init__(self):
        self.api_root_url = api_root_url
        self.session = requests.session()

    def get(self, path, **kwargs):
        return self.request(path, "GET", **kwargs)

    def post(self, path, **kwargs):
        return self.request(path, "POST", **kwargs)

    def request(self, path, method, **kwargs):
        self.request_log(path, method, **kwargs)
        if method == "GET":
            return self.session.get(self.api_root_url + path, **kwargs)
        if method == "POST":
            kwargs.get("data") != None
            return self.session.post(self.api_root_url + path, **kwargs)

    def request_log(self, path, method, **kwargs):
        logger.info("接口请求的地址>>>{}".format(self.api_root_url + path))
        logger.info("接口请求的方法>>>{}".format(method))
        logger.info("接口请求的参数>>>\n{}".format(kwargs))